package com.kd.xjoa.scheduled.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kd.xjoa.scheduled.domain.SumCompCheckDaily;
import com.kd.xjoa.scheduled.domain.WordCombination;
import com.kd.xjoa.scheduled.mapper.DictionaryMapper;
import com.kd.xjoa.scheduled.mapper.SumCheckDailyMapper;
import com.kd.xjoa.scheduled.search.bean.IndexParam;
import com.kd.xjoa.scheduled.service.CombService;
import com.kd.xjoa.scheduled.utils.ScheduledCommonUtil;
import com.kd.yq.common.utils.CommonUtils;

@Service
public class CombServiceImpl implements CombService{

	private static final Log logger = LogFactory.getLog(CombServiceImpl.class);

	@Autowired
	private SumCheckDailyMapper sumCheckDailyMapper;
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	private static final String DIC_LEDGER_YPE="db_oltp.tb_trs_ledger_assign.ledger_type";
	
	@Override
	public Integer totalCombMonitor(String snapDate) {
		int result = 0;
		if(StringUtils.isBlank(snapDate)){
			return result;
		}
		String regex = "\\|";
		IndexParam param = null;
		StringBuffer wordsb = null;
		String[] words = null;
		int totalCounts = 0;
		List<SumCompCheckDaily> list = new ArrayList<SumCompCheckDaily>();
		try {
			List<WordCombination> combs = sumCheckDailyMapper.listWordCombination();
			if(CommonUtils.isBlank(combs)){
				logger.info("=========专题数量查询为空");
				return result;
			}
			SumCompCheckDaily sumCompCheckDaily = null;
			for (WordCombination wordCombination : combs) {
				if(StringUtils.isBlank(wordCombination.getCn_words())){
					logger.info("=========专题："+wordCombination.getComb_desc()+",词组为空");
					continue;
				}
				sumCompCheckDaily = new SumCompCheckDaily();
				param = new IndexParam();
				wordsb = new StringBuffer();
				words = wordCombination.getCn_words().split(regex);
				for (int i = 0; i < words.length; i++) {
					if(i > 150) break;
					wordsb.append(words[i]).append(",");
				}
				if(wordsb.length() > 0){
					wordsb.deleteCharAt(wordsb.length()-1);
				}
				param.setContent(wordsb.toString());
				param.setPercentage(1);
				//初始化查询时间戳
				ScheduledCommonUtil.initDateTime(param, snapDate);
				//统计总数
				totalCounts = ScheduledCommonUtil.indexSearch(param);
				//返回结构为-1说明索引调用失败
				if(totalCounts == -1){
					logger.error("zt:"+wordCombination.getComb_desc()+",words:"+wordsb.toString()+",index search error, result null ");
					continue;
				}
				logger.info("zt:"+wordCombination.getComb_desc()+"result counts:"+totalCounts);
				sumCompCheckDaily.setSnap_date(Integer.valueOf(snapDate));
				sumCompCheckDaily.setComp_id(wordCombination.getComb_comp_id());
				//看郭经理提供的shell脚本，专题设置的为1，来源类型（fb、wx、wb等）设置的为2
				sumCompCheckDaily.setCheck_item_group("1");
				sumCompCheckDaily.setCheck_item(String.valueOf(wordCombination.getComb_no()));
				sumCompCheckDaily.setTotal_amt(totalCounts);
				list.add(sumCompCheckDaily);
			}
		} catch (Exception e) {
			logger.error("totalCombMonitor error ");
			e.printStackTrace();
		}finally{
			if(CommonUtils.isNotBlank(list)){
				result = sumCheckDailyMapper.batchAddSumCompCheckDaily(list);
				logger.info("totalCombMonitor, repalce into success,counts: "+result);
			}
		}
		return result;
	}

}
